<template>
  <div>
    <Mytable :arr='list'>
      <template #header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="scope">
        <td>{{scope.row.id}}</td>
        <td>{{scope.row.goods_name}}</td>
        <td>{{scope.row.goods_price}}</td>
        <td>
          <!-- 加一个按钮 1.点击按钮出现输入框 -->
          <input
            v-focus
            style="width: 100px;"
            type="text"
            class="tag-input form-control"
            v-if="scope.row.inputVisible"
            @blur="scope.row.inputVisible = false"
            @keydown.enter="entre(scope.row)"
            v-model="scope.row.inputValue"
            @keydown.esc="scope.row.inputValue=''"
          >
          <button
            v-else
            class="btn btn-primary btn-sm add-tag"
            @click="scope.row.inputVisible = true"
          >+Tag</button>

          <span
            v-for="(arr,ind) in scope.row.tags"
            :key="ind"
            class="badge badge-warning"
          >{{arr}}</span>
        </td>
        <td>
          <button
            class="btn btn-danger btn-sm"
            @click="delBtn(scope.index)"
          >删除</button>
        </td>
      </template>
    </Mytable>
  </div>
</template>

<script>
import axios from 'axios'
import Mytable from '../components/Mytable'
axios.defaults.baseURL = 'https://www.escook.cn'
export default {
  components: {
    Mytable
  },
  data () {
    return {
      list: []
    }
  },
  created () {
    axios({
      url: '/api/goods',
    }).then(res => {
      console.log(res);
      this.list = res.data.data
    })
  },
  methods: {
    delBtn (index) {
      // console.log(index)
      confirm('您确认删除吗?')
      this.list.splice(index, 1)
    },
    // 键盘按下
    entre (obj) {
      console.log(obj.inputValue);
      if (obj.inputValue.trim().length === 0) {
        alert('不能为空')
        return
      }
      obj.tags.push(obj.inputValue)
      obj.inputValue = ''
    }
  }
}
</script>

<style scoped>
.btn {
  display: block;
}
</style>